﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Runtime.Serialization.Formatters.Soap;
namespace ChinaClipping.ClippingMaster
{
    public partial class Dlg_KeywordListRecovery : Form
    {
        XmlDataDocument xmldoc = new XmlDataDocument();
        private SoapFormatter _soapformatter=new SoapFormatter ();
        private static Dlg_KeywordListRecovery _dlg = new Dlg_KeywordListRecovery();
        private Dlg_KeywordListRecovery()
        {
            InitializeComponent();
            bool s = false;
            try
            {
                xmldoc.Load(MySettings.KeywordListBakFile);
                s = true;
            }
            catch { }
            if (s == false)
            {
                try
                {
                    xmldoc.Load(MySettings.KeywordListBakFile2);
                    s = true;
                }
                catch { }
            }
            if (s == false)
            {
                MessageBox.Show("加载备份文件失败！");
                this.Hide();
            }
            foreach (XmlNode xn in xmldoc.SelectNodes("KeywordListBak/KeywordList"))
            {
                if (!string.IsNullOrEmpty(xn.Attributes["Date"].Value) && !string.IsNullOrEmpty(xn.InnerText))
                {
                    listBox_KeywordListDate.Items.Add(xn.Attributes["Date"].Value);
                }
            }
        }
        public static Dlg_KeywordListRecovery GetDialog()
        {
            return _dlg;
        }
        private void Dlg_KeywordListRecovery_Load(object sender, EventArgs e)
        {
            
           

        }

        private void listBox_KeywordListDate_SelectedIndexChanged(object sender, EventArgs e)
        {
         string dt=  listBox_KeywordListDate.SelectedItem.ToString();
         XmlNode xn = xmldoc.SelectSingleNode(string.Format("KeywordListBak/KeywordList[@Date='{0}']", dt));
         if (xn != null)
         {
             string kl = xn.InnerText;
             byte[] bt = Convert.FromBase64String(kl);
             
             Stream stream=new MemoryStream (bt);
             TreeNode tn = _soapformatter.Deserialize(stream) as TreeNode;

             if (tn != null)
             {
                 setTreeNodeIcon(tn);
                 this.treeView_KeywordList.BeginUpdate();
                 this.treeView_KeywordList.Nodes.Clear();
                 this.treeView_KeywordList.Nodes.Add(tn);
                 this.treeView_KeywordList.ExpandAll();
                 this.treeView_KeywordList.EndUpdate();
                 button_OK.Enabled = true;
             }
         }
        }
        private void setTreeNodeIcon(TreeNode treeNode)
        {
            if (treeNode.Tag == null)
            {
                treeNode.ImageIndex = 1;
                treeNode.SelectedImageIndex = 3;
            }
            else
            {
                treeNode.ImageIndex = 0;
                treeNode.SelectedImageIndex = 0;
            }
            foreach (TreeNode tn in treeNode.Nodes)
            {
                setTreeNodeIcon(tn);
            }
        }

        public TreeNode KeywordListNode
        {
            get 
            {
                if (this.treeView_KeywordList.Nodes.Count == 1)
                {
                    return this.treeView_KeywordList.Nodes[0].Clone() as TreeNode;
                }
                else
                    return null;
            }
        }

        private void button_OK_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("要将关键词列表恢复到选定的版本吗？", "确认操作", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                this.DialogResult = DialogResult.OK;
                this.Hide();
            }
        }

        private void button_Cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Hide();
        }

        private void Dlg_KeywordListRecovery_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
                this.Hide();
            }
        }


    }
}
